package com.ztesoft.huangfu.learning.springboot.springbootmongodb.controller;

import com.ztesoft.huangfu.learning.springboot.springbootmongodb.entity.Customer;
import com.ztesoft.huangfu.learning.springboot.springbootmongodb.service.MongoCustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Author: huangfu.daohui
 * Date: 2018/5/28
 * Time: 18:50
 * Version: 1.0
 * Description:mongodb  操作
 * 参考：https://www.xncoding.com/2017/07/04/spring/sb-mongodb.html
 **/
@RestController
@RequestMapping("/mongo")
public class MongoController {

    @Autowired
    private MongoCustomerService mongoService;

    @DeleteMapping("/deleteAll")
    public void deleteAll() {
        mongoService.deleteAll();
        for (Customer customer : mongoService.findAll()) {
            System.out.println("customer:" + customer);
        }
    }

    @PostMapping("/save")
    public void save() {
        mongoService.save(new Customer("Alice", "Smith"));
        mongoService.save(new Customer("Bob", "Smith"));
        for (Customer customer : mongoService.findAll()) {
            System.out.println("customer:" + customer);
        }
    }

    @GetMapping("/getAll")
    public Iterable<Customer> getAll() {
        Iterable<Customer> customerList = mongoService.findAll();
        for (Customer customer : mongoService.findAll()) {
            System.out.println("customer:" + customer);
        }
        return customerList;
    }

    @GetMapping("/findByFirstName")
    public Customer findByFirstName(String firstName) {
        return mongoService.findByFirstName(firstName);
    }

    @GetMapping("/findByLastName")
    public List<Customer> findByLastName(String lastName) {
        return mongoService.findByLastName(lastName);

    }
}
